/**
 * Created by jiangtao on 2017/5/17.
 */

var webpack = require('webpack');
var path = require('path');
var fs= require('fs');

var HtmlWebpackPlugin = require('html-webpack-plugin');

var ExtractTextPlugin = require("extract-text-webpack-plugin");

//页面
var pages = require("./webpack.page.config");

var baseConfigInit = function ( ENV ) {

    ENV = ENV || "DEV";

    //如果是正式环境的打包,那么就在css、js、图片、字体文件后面加上hash
    var Hash8 = ENV == "PROD" ? "-[hash:8]" : "" ;

    //基本配置
    var webpackBaseConfig =  {
        // 入口
        entry: {
            common: [
                'vue',
                'vue-router',
                'vuex',
                 path.join(  __dirname  , "src/common/common.js")
            ],
        },
        // 输出
        output: {
            path:path.join( __dirname , "/dist"),
            publicPath:"",
            filename:"js/[name]" + Hash8 + ".js"
        },
        // 加载器
        module: {
            rules: [
                {
                    test: /\.vue$/,
                    use: 'vue-loader',
                    exclude: function(path) {
                        // 路径中含有 node_modules 的就不去解析。
                        var isNpmModule = !!path.match(/node_modules/);
                        return isNpmModule;
                    }
                },
                {
                    test: /\.js?$/,
                    use: 'babel-loader',
                    exclude: function(path) {
                        // 路径中含有 node_modules 的就不去解析。
                        var isNpmModule = !!path.match(/node_modules/);
                        return isNpmModule;
                    },
                },
                //加载css、scss文件
                {
                    test: /\.(css|scss)$/,
                    use: ExtractTextPlugin.extract({
                        fallback:"style-loader",
                        use:[
                            'css-loader',
                            'postcss-loader',
                            'sass-loader'
                        ]
                    })
                },
                //加载图片
                {
                    test: /\.(png|jpg|svg|gif)$/,
                    use: "url-loader?limit=20480&name=images/[name]" + Hash8 + ".[ext]"
                },
                {
                    test: /\.jpeg?$/,
                    use: "file-loader?limit=20480&name=images/[name]" + Hash8 + ".[ext]"
                },
                //加载字体
                {
                    test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                    use: "url-loader?limit=10000&mimetype=application/font-woff&name=fonts/[name]" + Hash8 + ".[ext]"
                },
                {
                    test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                    use: "file-loader?name=fonts/[name]" + Hash8 + ".[ext]"
                }
            ],

        },
        resolve: {
            // require时省略的扩展名，如：require('module') 不需要module.js
            extensions: ['.js', '.vue'],
            // 别名，可以直接使用别名来代表设定的路径以及其他
            alias: {
                'src': path.resolve(__dirname, '/src'),
                'img': path.resolve(__dirname, '/src/images'),
                'components': path.resolve(__dirname, '/src/components')
            }
        },
        plugins: [
            new webpack.optimize.CommonsChunkPlugin({
                name:"common",
                filename: "js/common" + Hash8 + ".js"
            }),

            //提取css,将它放在css文件夹下
            new ExtractTextPlugin({
                allChunks: true,
                filename: "css/[name]" + Hash8 + ".css"
            })
        ]
    };

    pages.forEach(function ( item ) {
        //将入口添加到entry中
        webpackBaseConfig.entry[ item.chunk ] = item.viewPath + item.chunk + '.js';
        webpackBaseConfig.plugins.push( new HtmlWebpackPlugin({
            title:"",
            filename: item.filename + ".html" ,
            template:  "server.template.html",
            chunks: [ "common", item.chunk ],
            inject:false,
        }));
    });

    return webpackBaseConfig;
};

module.exports = baseConfigInit;